<?php

class ArticleController extends GxController {

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id, 'Article'),
        ));
    }

    public function actionCreate() {
        $model = new Article;
        $this->savecmsDescription($model);
        $this->render('create', array('model' => $model));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id, 'Article');
        $this->savecmsDescription($model);
        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionviewarticles($id, $seo_title) {
        $this->layout = 'innerpage';
        $this->render('articles-page', array(
            'model' => $this->loadModel($id, 'Article'),
        ));
    }

    public function actionDelete($id) {
        if (Yii::app()->getRequest()->getIsPostRequest()) {
            $this->loadModel($id, 'Article')->delete();

            if (!Yii::app()->getRequest()->getIsAjaxRequest())
                $this->redirect(array('admin'));
        } else
            throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
    }

    public function actionAdmin() {
        $model = new Article('search');
        $model->unsetAttributes();

        if (isset($_GET['Article']))
            $model->setAttributes($_GET['Article']);

        $this->render('admin', array(
            'model' => $model,
        ));
    }

    private function savecmsDescription($model) {
        $uploadeDir = YiiBase::getPathOfAlias('webroot') . ECSconfig::getUploadDir() . 'article-image/';
        $search = array(' ', '&', '/');
        $replace = array('-', 'and', '-');
        $rfilename = '';
        if (isset($_POST['Article'])) {
            $db_logo = $model->photo_name;
            $model->setattributes($_POST['Article']);
            $model->setAttribute('seo_title', trim(strtolower(str_replace($search, $replace, $_POST['Article']['title']))));
            $model->setAttribute('photo_name', $rfilename);
            $model->setAttribute('create_time ', date('Y-m-d'));
            if ($model->validate()) {
                if (!empty($_FILES['Article']['name']['photo_name'])) {
                    $model->photo_name = CUploadedFile::getInstance($model, 'photo_name');
                    $fileInfo = pathinfo($model->photo_name);
                    $ext = $fileInfo['extension'];
                    $image = $_FILES['Article']['tmp_name'];
                    $mainTempImages = $image['photo_name'];
                    $rfilename = guid() . "." . strtolower($ext);
                    $uploadnewDir = $uploadeDir . $rfilename;
                    $model->photo_name->saveAs($uploadnewDir);
                    $db_logo = $rfilename;
                }
                $model->setAttribute('photo_name', $db_logo);
                if ($model->save()) {
                    $this->redirect(array('view', 'id' => $model->id));
                }
            }
        }
    }

}
